import requests
import json
import os
import logging
import uuid  # 用于生成唯一标识符

# 常量
SIGN_IN_URL = "https://apiff14risingstones.web.sdo.com/api/home/sign/signIn"

# 配置日志记录
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("[石之家小助手]")

def generate_tempsuid():
    # 生成一个特定格式的 tempsuid
    tempsuid = str(uuid.uuid4())  # 使用uuid模块生成唯一标识符
    tempsuid_parts = tempsuid.split("-")  # 将uuid字符串按照"-"分割成多个部分
    # 使用map函数对每个部分进行格式化，确保长度满足要求
    formatted_tempsuid = "-".join(map(lambda x: x[:8] if len(x) > 8 else x, tempsuid_parts))
    return formatted_tempsuid

def get_cookies_from_json():
    # 获取当前运行的目录
    current_dir = os.getcwd()
    # 构建base.json文件的路径
    json_path = os.path.join(current_dir, "data.json")
    # 读取JSON文件
    with open(json_path, 'r') as file:
        data = json.load(file)
    # 获取Cookie部分的值
    cookies = data.get("Cookie", "")
    return cookies


def send_post_request(tempsuid, cookies):
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36 Edg/96.0.1054.62",
        "Cookie": cookies,
        "tempsuid": tempsuid  # 添加生成的 tempsuid 到请求头中
    }
    
    logger.info("正在签到...")
    
    try:
        response = requests.post(SIGN_IN_URL, headers=headers)
        if response.status_code == 200:
            logger.info(f"签到成功！{response.json()}")
            logger.debug(response.text)
        else:
            logger.error(f"签到失败！状态码：{response.status_code}，错误信息：{response.text}")
    except Exception as e:
        logger.error(f"签到失败！发生异常：{e}")

def main():
    tempsuid = generate_tempsuid()  # 生成特定格式的 tempsuid
    print(tempsuid)
    cookies = get_cookies_from_json()  # 从 data.json 文件获取 cookies
    
    send_post_request(tempsuid, cookies)
    # 给出提示信息
    input("请按下任意键继续...")

if __name__ == "__main__":
    main()
